<?php

namespace app\checkbill\logic;
use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use app\api\logic\TrackLogic;
use think\facade\Db;
class BillLogic
{


    /**
     * @desc 顺丰3月账单对账
     * @author  luckyXiong
     */
    public function import_SF_detail($params){
        try{
            $result = CommonSystemImportService::staticModel()->operate_import_data([
                    $this,//当前对应的service
                    'save',//类型: template 下载模板; save :保存 导入
                    CommonSystemImportService::TYPE_IMPORT_SF_PAGE,
                    1,
                    $params
                ]
            );
        }catch (\Exception $e) {
            return $this->jsonMessage(201, false, $e->getMessage(), null);
        }
    }



    /**
     * @desc 将导入数据写入目标表
     * @author xx
     * @date 2024-03-21
     */
    public function import_sf_info($params=[]){
        htmlLog(['params'=>$params],OPERATE_LOG . date('Ym') . '/import_sf_info/' . date('Ymd') . '.txt');
        $key        = 'import_sf_info:'.time();
        $redis_lock = new RedissLock();
        $lock       = $redis_lock::lock($key, $redis_lock::UNBLOCK, 500);
        if(!$lock){
            return ['code'=>201,'msg'=>'已处理过'];
        }
        $reference_no             = getIsset($params,'reference_no');
        $waybill_no               = getIsset($params,'waybill_no');
        $postal                   = getIsset($params,'postal');
        $total_weight             = getIsset($params,'total_weight');
        $pieces                   = getIsset($params,'pieces');
        $operating_fee            = getIsset($params,'operating_fee');
        //数据入库逻辑
        $info = [
            'reference_no'        =>$reference_no,
            'waybill_no'          =>$waybill_no,
            'postal'              =>$postal,
            'total_weight'        =>$total_weight,
            'pieces'              =>$pieces,
            'operating_fee'       =>$operating_fee,
        ];
        $res = Db::name('checkbill_sf')->insertGetId($info);
        $lock->unlock();
        $info['status'] = $res == 1 ? 1:2;
        return  $info;
    }


    /**
     * @param $row
     * @return string[]
     * @desc 数据title 导入组装数据结构
     */
    public function get_excel_row_data($row){
        return [
            'reference_no'       => empty($row['A'])? '': trim($row['A']),
            'waybill_no'         => empty($row['B'])? '': trim($row['B']),
            'postal'             => empty($row['C'])? '': trim($row['C']),
            'total_weight'       => empty($row['D'])? '': trim($row['D']),
            'pieces'             => empty($row['E'])? '': trim($row['E']),
            'operating_fee'      => empty($row['F'])? '': trim($row['F']),
        ];
    }

    /**
     * @param 生产方法
     * @return string[]
     * @desc 数据title 导入组装数据结构
     */
    public function importSFProduce(){
        $list = Db::name('checkbill_sf')->select()->toArray();
        //数据推入队列处理;
        foreach($list as $v){
            MqTaskPushLogic::mq_task_push(
                '',
                MqQueueName::QUEUE_TIK_ORDER_MONTH_SF_QUEUE,
                $v,
                false
            );
        }
    }


    /**
     * @param 消费方法
     * @return string[]
     * @desc 数据title 导入组装数据结构
     */
    public function importSFComsumer($checkbill_sf){
        $checkbill_sf['reference_no'] = trim($checkbill_sf['reference_no']);
        $checkbill_sf['postal'] = trim($checkbill_sf['postal']);
        $checkbill_sf['total_weight'] = trim($checkbill_sf['total_weight']);

        $return = (new TrackLogic())->get_order_check($checkbill_sf['reference_no']);
        // dump($return);die;
        //折后运费
        if(isset($return['total_weight']) && !empty($return['total_weight'])){
            //总重量修改成长*宽*高/6000 换算成g
            $return['total_weight'] = (float)number_format($checkbill_sf['total_weight']*1000, 2, '.', '');
            //分区
            $postal = (int)substr($checkbill_sf['postal'],0,3);
            $recon_sf_zone = Db::name('checkbill_sfzone')
                ->where('postal_code_start',"<=",$postal)
                ->where('postal_code_end',">=",$postal)
                ->find();
            if(!empty($recon_sf_zone)){
                $checkbill_sf['zone_id'] = $recon_sf_zone['zone_id'];
            }else{
                $checkbill_sf['zone_id'] = 0;
            }

            $weight_range_id = $this->weight_range_v2($return['total_weight']);
            $zone_weight_v2 = Db::name('checkbill_sfzoneweight')
                ->where('zone_id',$checkbill_sf['zone_id'])
                ->where('weight_range_id',$weight_range_id)
                ->find();
            if(!empty($zone_weight_v2)){
                $discounted_shipping_new = $zone_weight_v2['freight_cost'];
            }else{
                $discounted_shipping_new = 0;
            }
            //操作费
            $operating_fee_news = 0;
            $multiple_pieces = $return['total_num'] - 1;//多件
            if($return['type_menue'] == 0){//一品
                if($multiple_pieces >=1){
                    $operating_fee_news = (float)number_format($multiple_pieces*2.38+3.24, 2, '.', '');
                }else{
                    $operating_fee_news = 3.24;
                }
            }
            if($return['type_menue'] == 1){//多品
                if($multiple_pieces >=1){
                    $operating_fee_news = (float)number_format($multiple_pieces*2.57+3.24, 2, '.', '');
                }else{
                    $operating_fee_news = 3.24;
                }
            }
            //最终数据
            // $update['total_weight_new'] = $return['total_weight'];
            // $update['height_new'] = $checkbill_sf['total_height'];
            $update['total_weight_new'] = (float)number_format($return['total_weight']/1000, 2, '.', '');
            $update['pieces_new'] = $return['total_num'];
            // $update['discounted_shipping_new'] = $discounted_shipping_new;
            $update['operating_fee_news'] = $operating_fee_news;
            $update['reconciliation_status'] = 1;
            $update['zone_id_new'] = $checkbill_sf['zone_id'];
            $update['discounted_shipping_new'] = $discounted_shipping_new;
            Db::name('checkbill_sf')
                ->where('id',$checkbill_sf['id'])
                ->update($update);
            $data = [
                'code' => 1,
                'msg' => "",
                'time' => time(),
                'data' => $update,
            ];
            return true;
        }else{
            $update = array();
            $update['reconciliation_status'] = 2;
            Db::name('checkbill_sf')
                ->where('id',$checkbill_sf['id'])
                ->update($update);
        }
        return true;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function weight_range_v2($weight){
        $weight_range_id = 0;
        if ($weight >= 1 && $weight <= 28) {
            $weight_range_id = 1;
        } elseif ($weight >= 29 && $weight <= 56) {
            $weight_range_id = 2;
        } elseif ($weight >= 57 && $weight <= 84) {
            $weight_range_id = 3;
        } elseif ($weight >= 85 && $weight <= 112) {
            $weight_range_id = 4;
        } elseif ($weight >= 113 && $weight <= 140) {
            $weight_range_id = 5;
        } elseif ($weight >= 141 && $weight <= 168) {
            $weight_range_id = 6;
        } elseif ($weight >= 169 && $weight <= 196) {
            $weight_range_id = 7;
        } elseif ($weight >= 197 && $weight <= 224) {
            $weight_range_id = 8;
        } elseif ($weight >= 225 && $weight <= 252) {
            $weight_range_id = 9;
        } elseif ($weight >= 253 && $weight <= 280) {
            $weight_range_id = 10;
        } elseif ($weight >= 281 && $weight <= 308) {
            $weight_range_id = 11;
        } elseif ($weight >= 309 && $weight <= 336) {
            $weight_range_id = 12;
        } elseif ($weight >= 337 && $weight <= 364) {
            $weight_range_id = 13;
        } elseif ($weight >= 365 && $weight <= 392) {
            $weight_range_id = 14;
        } elseif ($weight >= 393 && $weight <= 420) {
            $weight_range_id = 15;
        } elseif ($weight >= 421 && $weight <= 448) {
            $weight_range_id = 16;
        }elseif ($weight >= 449 && $weight <= 450) {
            $weight_range_id = 17;
        }elseif ($weight >= 451 && $weight <= 900) {
            $weight_range_id = 18;
        }elseif ($weight >= 901 && $weight <= 1350) {
            $weight_range_id = 19;
        }elseif ($weight >= 1351 && $weight <= 1800) {
            $weight_range_id = 20;
        }elseif ($weight >= 1801 && $weight <= 2250) {
            $weight_range_id = 21;
        }elseif ($weight >= 2251 && $weight <= 2700) {
            $weight_range_id = 22;
        }elseif ($weight >= 2701 && $weight <= 3150) {
            $weight_range_id = 23;
        }elseif ($weight >= 3151 && $weight <= 3600) {
            $weight_range_id = 24;
        }elseif ($weight >= 3601 && $weight <= 4050) {
            $weight_range_id = 25;
        }elseif ($weight >= 4051) {
            $weight_range_id = 26;
        }
        return $weight_range_id;
    }



}